package com.gome.security.util.cert;

import java.security.*;
import java.io.*;
import java.util.*;
import java.security.*;
import java.security.cert.*;
import sun.security.x509.*;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;

public class FF {
	
	
	public static void main(String[] args) throws Exception {
		
		CertificateFactory cf = CertificateFactory.getInstance("X.509");
		FileInputStream in = new FileInputStream("d:/keydsa/gomerb.csr");
		java.security.cert.Certificate c = cf.generateCertificate(in);
		X509Certificate t = (X509Certificate) c;
		byte[] encod2 = c.getEncoded();
		
		String alias = "rb";
		String pass = "123456";
		FileInputStream ins = new FileInputStream(".keystore");
		KeyStore ks = KeyStore.getInstance("JKS");
		ks.load(ins, pass.toCharArray());
		java.security.cert.Certificate c1 = ks.getCertificate(alias);// alias为条目的别名
		
		
		Enumeration e=ks.aliases();
		while(e.hasMoreElements()){
		 java.security.cert.Certificate c2=ks.getCertificate((String)e.nextElement());
		 }
		
		
		
		
		
		
		
		X509CertImpl cimp = new X509CertImpl(encod2);// 用该编码创建X509CertImpl类型对象
		//X509CertInfo cinfo = (X509CertInfo) cimp.get(X509CertImpl.NAME + "." + X509CertImpl.INFO);// 获取X509CertInfo对象
		System.out.println("输出证书信息:\n" + c.toString());
		System.out.println("版本号:" + t.getVersion());
		System.out.println("序列号:" + t.getSerialNumber().toString(16));
		System.out.println("主体名：" + t.getSubjectDN());
		System.out.println("签发者：" + t.getIssuerDN());
		System.out.println("有效期：" + t.getNotBefore());
		System.out.println("签名算法：" + t.getSigAlgName());
		byte[] sig = t.getSignature();// 签名值
		PublicKey pk = t.getPublicKey();
		byte[] pkenc = pk.getEncoded();
		System.out.println("公钥");
		for (int i = 0; i < pkenc.length; i++)
			System.out.print(pkenc[i] + ",");
	}

}
